//
//  GYOrderViewController.m
//  GoodLuck
//
//  Created by 郭为勇 on 2018/3/17.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *orderCellIdentifer = @"orderCellIdentifer";
static NSString *footViewIdentifier = @"footViewIdentifier";


#import "GYOrderViewController.h"
#import "GYNoAddressView.h"
#import "GYSearchViewController.h"
#import "PopoverAnimation.h"
#import "GYBasePopoverViewController.h"
#import "GYSystemMessageViewController.h"
#import "GYNetWorkTools.h"
#import "GYOrderList.h"
#import "GYOrderCell.h"
#import "GYOrderDetailsViewController.h"
#import "GYOrderEvaluateViewController.h"
#import "GYPayMethodSelectView.h"
//#import "GYPayViewController.h"
#import "GYShopingCartViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"


static NSString *page = @"20";

@interface GYOrderViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btuLists;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *markLeftMarginCons;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UITextField *searchTF;

/** 没有订单的视图 */
@property (nonatomic, strong) GYNoAddressView *noOrderView;

/** 动画代理 */
@property (nonatomic, strong) PopoverAnimation *animationDelegate;

/** 表格的数据源 */
@property (nonatomic, strong) NSMutableArray *orderList;

/** 当前选中的分类订单 */
@property (nonatomic, strong) NSString *currentState_type;

/** 代表当前页数 */
@property (nonatomic, assign) NSInteger currentIndex;

@property (nonatomic, getter=isHasmore) BOOL hasmore;

/** 支付视图 */
@property (nonatomic, strong) GYPayMethodSelectView *payMethodSelectView;

/** 根据订单号和店铺名搜索结果 */
@property (nonatomic, strong) NSMutableArray *searchResults;

//代表是否搜索
@property (nonatomic, getter=isSearchSatte) BOOL searchState;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionBottomMarginCons;

/** 当前支付单号 */
@property (nonatomic, strong) NSString *pay_sn;

/** 当前支付模型 */
@property (nonatomic, strong) GYPayment *payModel;

/** 当前支付通知URL */
@property (nonatomic, strong) NSString *notifyUrl;
@end

@implementation GYOrderViewController

- (IBAction)onCLick:(UIButton *)sender {
    
    [self setBtuState:sender.tag];
}

//设置按钮的状态
- (void)setBtuState:(NSInteger)index {
    self.currentSelectIndex = index;
    self.orderList = nil;
    self.currentIndex = 1;
    self.hasmore = YES;
    self.searchState = NO;
    NSString *state_type= @"";
    switch (index) {
        case 1:
        {
            state_type = @"";
        }
            break;
            
        case 2:
        {
            
            state_type = @"state_new";
            
        }
            break;
            
        case 3:
        {
            
            state_type = @"state_deliver";
        }
            break;
            
        case 4:
        {
            
            state_type = @"state_send";
        }
            break;
            
        case 5:
        {
            state_type = @"state_noeval";
        }
            break;
            
        default:
            break;
    }
    
    self.currentState_type = state_type;
    
    //加载网络数据
    [self loadOrderList:state_type order_key:@"" curpage:[NSString stringWithFormat:@"%zd",self.currentIndex] isLoaindingView:YES];
    
    for (UIButton *btu in self.btuLists) {
        if (btu.tag == index) {
            //代表点击的哪个按钮
            btu.selected = !btu.isSelected;
            [UIView animateWithDuration:0.5 animations:^{
                self.markLeftMarginCons.constant = btu.frame.origin.x;
            }];
            
            continue;
        }
        btu.selected = NO;
    }
}

- (void)loadOrderList:(NSString *)state_type order_key:(NSString *)order_key curpage:(NSString *)curpage isLoaindingView:(BOOL)show {
    if (self.isHasmore) {
        if (show) {
            [EasyLodingView showLodingText:@"正在加载..." config:^EasyLodingConfig *{
                EasyLodingConfig *config = [EasyLodingConfig shared];
                config.showOnWindow = YES;
                
                return config;
            }];
        }
        [GYNetWorkTools requestOrderList:state_type order_key:order_key page:page curpage:curpage success:^(NSDictionary *obj) {
            //NSLog(@"==========%@",obj);
            int code = [[obj objectForKey:@"code"] intValue];
            NSDictionary *datas = [obj objectForKey:@"datas"];
            if (code == 200) {
                if (show) {
                    [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
                }
                self.currentIndex += 1;
                self.hasmore = [[obj objectForKey:@"hasmore"] boolValue];
                NSArray *order_group_list = [datas objectForKey:@"order_group_list"];
                if (order_group_list.count == 0) {
                    if (self.noOrderView == nil) {
                        [self getNoOrderView];
                    }else {
                        self.noOrderView.hidden = NO;
                    }
                }else {
                    self.noOrderView.hidden = YES;
                    [self.orderList addObjectsFromArray:[GYOrderList loadOrderList:order_group_list]];
                    [self.collectionView reloadData];
                }
            }else if (code == 400) {
                if (show) {
                    [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
                    [EasyTextView showText:@"加载出错"];
                }
                NSLog(@"======%@",[datas objectForKey:@"error"]);
            }
        } error:^(NSError *error) {
            if (show) {
                [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
                [EasyTextView showText:@"加载出错"];
            }
            NSLog(@"loadOrderListFailed====%@",error);
        }];
    }else {
        [EasyTextView showText:@"没有更多数据了!"];
        NSLog(@"没有数据可以加载了....");
    }
}

- (IBAction)searchBtuClick:(UIButton *)sender {
    [self.searchTF resignFirstResponder];
    if (!self.isSearchSatte) {
        if (self.searchTF.text.length == 0) {
            [EasyTextView showText:@"请输入店铺名称或则订单号!"];
            return;
        }
        //暂时搜索结果
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            for (GYOrderList *model in self.orderList) {
                for (GYOrder *order in model.order_list) {
                    if ([order.order_sn containsString:self.searchTF.text] || [order.store_name containsString:self.searchTF.text]) {
                        [self.searchResults addObject:model];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                self.searchState = YES;
                if (self.searchResults.count == 0) {
                    if (self.noOrderView == nil) {
                        [self getNoOrderView];
                    }else {
                        self.noOrderView.hidden = NO;
                    }
                }else {
                    self.noOrderView.hidden = YES;
                    [self.collectionView reloadData];
                }
            });
        });
    }else {
        self.searchState = NO;
        if (self.orderList.count != 0) {
            self.noOrderView.hidden = YES;
            [self.collectionView reloadData];
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.searchTF resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionBottomMarginCons.constant = (iPhoneX ? 44 : 0);
    //初始化UI
    [self setupUI];
}
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    self.markLeftMarginCons.constant = [self.btuLists[self.currentSelectIndex-1] frame].origin.x;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //设置按钮的状态
    [self setBtuState:self.currentSelectIndex];
}


- (void)setupUI {
    self.navigationItem.title = @"订单列表";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"more_b" tagret:self action:@selector(moreBtuClick) button:nil];
    
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"GYOrderCell" bundle:nil] forCellWithReuseIdentifier:orderCellIdentifer];
    //注册尾视图
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footViewIdentifier];
    
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    //注册监听支付
    [self registerPayNotification];
}

- (void)loadMoreData {
    [self.collectionView.mj_footer endRefreshing];
    [self loadOrderList:self.currentState_type order_key:@"" curpage:[NSString stringWithFormat:@"%zd",self.currentIndex] isLoaindingView:YES];
}

- (void)moreBtuClick {
    
    GYBasePopoverViewController *basePopoverVC = [[GYBasePopoverViewController alloc] initWithNibName:@"GYBasePopoverViewController" bundle:nil];
    __weak typeof(self) weakSelf = self;
    basePopoverVC.popoverViewSelectIndexCallBack = ^(NSInteger index) {
        switch (index) {
            case 0:
            {
                weakSelf.tabBarController.selectedIndex = 0;
                [weakSelf.tabBarController.tabBar setHidden:NO];
            }
                break;
                
            case 1:
            {
                GYSearchViewController *searchVC = [[GYSearchViewController alloc] init];
                [weakSelf.navigationController pushViewController:searchVC animated:YES];
            }
                break;
                
            case 2:
            {
                GYShopingCartViewController *shopingCartVC = [[GYShopingCartViewController alloc] initWithNibName:@"GYShopingCartViewController" bundle:nil];
                [weakSelf.navigationController pushViewController:shopingCartVC animated:YES];
            }
                break;
                
            case 3:
            {
                GYSystemMessageViewController *systemMessageVC = [[GYSystemMessageViewController alloc] initWithNibName:@"GYSystemMessageViewController" bundle:nil];
                
                [weakSelf.navigationController pushViewController:systemMessageVC animated:YES];

            }
                break;
                
            default:
                break;
        }
    };
    basePopoverVC.titles = @[@"首页",@"搜索",@"购物车",@"消息"];
    basePopoverVC.images = @[@"home_w",@"search_w",@"ff_icon3",@"message_w"];
    
    self.animationDelegate = [[PopoverAnimation alloc] init];
    self.animationDelegate.popoverFrame = CGRectMake(ScreenWidth-widthScale(130, iphone6_W)-5, 50, widthScale(130, iphone6_W), basePopoverVC.titles.count*50);
    basePopoverVC.transitioningDelegate = (id)weakSelf.animationDelegate;
    
    basePopoverVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:basePopoverVC animated:YES completion:nil];

}

#pragma mark -- UICollectionViewDelegate UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.isSearchSatte ? self.searchResults.count : self.orderList.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    GYOrderList *list = self.isSearchSatte ? self.searchResults[section] : self.orderList[section];
    
    return list.order_list.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GYOrderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:orderCellIdentifer forIndexPath:indexPath];
    GYOrderList *orderList = self.isSearchSatte ? self.searchResults[indexPath.section] : self.orderList[indexPath.section];
    cell.orderModel = orderList.order_list[indexPath.item];
    __weak typeof(self) weakSelf = self;
    cell.deleteOrderCallBack = ^{
        //删除成功之后应该刷新数据,直接改变数据源就可以了
        [weakSelf.orderList removeObjectAtIndex:indexPath.section];
        [collectionView reloadData];
    };
    
    cell.cancelOrderCallBack = ^() {
        //取消订单
        weakSelf.orderList = nil;
        weakSelf.hasmore = YES;
        weakSelf.currentIndex = 1;
        [weakSelf loadOrderList:self.currentState_type order_key:@"" curpage:[NSString stringWithFormat:@"%zd",self.currentIndex] isLoaindingView:NO];
    };

    cell.confirmOrEvaluateCallBack = ^(NSInteger orderState) {
        switch (orderState) {
            case 30:
            {
                //刷新界面
                weakSelf.orderList = nil;
                weakSelf.hasmore = YES;//修改
                weakSelf.currentIndex = 1;
                [weakSelf loadOrderList:self.currentState_type order_key:@"" curpage:[NSString stringWithFormat:@"%zd",self.currentIndex] isLoaindingView:NO];
            }
                break;
              
            case 40:
            {
                
                GYOrderEvaluateViewController *orderEvaluateVC = [[GYOrderEvaluateViewController alloc] initWithNibName:@"GYOrderEvaluateViewController" bundle:nil];
                orderEvaluateVC.orderGoodsList = [orderList.order_list[indexPath.item] extend_order_goods];
                [weakSelf.navigationController pushViewController:orderEvaluateVC animated:YES];
            }
                break;
                
            default:
                break;
        }
    };
    
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    GYOrderList *orderList = self.isSearchSatte ? self.searchResults[indexPath.section] : self.orderList[indexPath.section];
    GYOrderDetailsViewController *detailsVC = [[GYOrderDetailsViewController alloc] initWithNibName:@"GYOrderDetailsViewController" bundle:nil];
    detailsVC.order_id = [orderList.order_list[indexPath.item] order_id];
    [self.navigationController pushViewController:detailsVC animated:YES];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionFooter) {
        GYOrderList *orderList = self.isSearchSatte ? self.searchResults[indexPath.section] : self.orderList[indexPath.section];
            UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footViewIdentifier forIndexPath:indexPath];
        for (UIView *view in footView.subviews) {
            [view removeFromSuperview];
        }
//        UIButton *orderBtu = [footView viewWithTag:101];
        if (orderList.pay_amount != 0) {
            //if (orderBtu == nil) {
                UIButton *orderBtu = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, ScreenWidth-40, 44)];
                orderBtu.tag = indexPath.section;
                [orderBtu setBackgroundColor:RGBColor(0, 158, 214)];//RGBColor(68, 165, 25)
                [orderBtu setTitle:[NSString stringWithFormat:@"订单支付 (¥%0.2f)",orderList.pay_amount] forState:UIControlStateNormal];
                orderBtu.layer.cornerRadius = 4;
                [orderBtu addTarget:self action:@selector(confirmOrderBtuCLick:) forControlEvents:UIControlEventTouchUpInside];
                [footView addSubview:orderBtu];
//            }else {
//                [orderBtu setTitle:[NSString stringWithFormat:@"订单支付 (¥%0.2f)",orderList.pay_amount] forState:UIControlStateNormal];
//            }
        }
//        else {
//            [orderBtu removeFromSuperview];
//        }
            return footView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    GYOrderList *orderList = self.isSearchSatte ? self.searchResults[section] : self.orderList[section];
    if (orderList.pay_amount != 0) {
        return CGSizeMake(ScreenWidth, 80);
    }

    return CGSizeMake(ScreenWidth, 30);
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    GYOrderCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"GYOrderCell" owner:nil options:nil] firstObject];
    GYOrderList *orderList = self.isSearchSatte ? self.searchResults[indexPath.section] : self.orderList[indexPath.section];
    
    return CGSizeMake(ScreenWidth, [cell getHeight:orderList.order_list[indexPath.item]]);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.searchTF resignFirstResponder];
}


/**
 确认付款
 */
- (void)confirmOrderBtuCLick:(UIButton *)sender {
    
    GYOrderList *orderList = self.orderList[sender.tag];
    GYPayMethodSelectView *payMethodSelectView = [[[NSBundle mainBundle] loadNibNamed:@"GYPayMethodSelectView" owner:self options:nil] firstObject];
    payMethodSelectView.frame = [UIScreen mainScreen].bounds;
    payMethodSelectView.pay_sn = orderList.pay_sn;
    payMethodSelectView.orderPay = NO;
    payMethodSelectView.goods_price = [NSString stringWithFormat:@"%0.2f",orderList.pay_amount];
    
    __weak typeof(self) weakSelf = self;
    payMethodSelectView.returnPayMethodCallBack = ^(GYPayment *payMethod, NSString *notifyUrl) {
        //1.首先赋值
        weakSelf.pay_sn = orderList.pay_sn;
        weakSelf.payModel = payMethod;
        weakSelf.notifyUrl = notifyUrl;
        
        //2.发起支付
        //[self WeiXinPaySuccess];
        [self launchPay:payMethod pay_sn:orderList.pay_sn notifyUrl:notifyUrl];
//        GYPayViewController *payVC = [[GYPayViewController alloc] initWithNibName:@"GYPayViewController" bundle:nil];
//        payVC.model = payMethod;
//        payVC.pay_sn = orderList.pay_sn;
//        payVC.notifyUrl = notifyUrl;
//        payVC.orderPay = YES;
//        payVC.order_id = [orderList.order_list firstObject];
//
//        [weakSelf.navigationController pushViewController:payVC animated:YES];
//
        [weakSelf.payMethodSelectView removeFromSuperview];
    };
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:payMethodSelectView];
    weakSelf.payMethodSelectView = payMethodSelectView;

}



#pragma mark -- 封装的方法

- (void)getNoOrderView {
    
    GYNoAddressView *noOrderView = [[[NSBundle mainBundle] loadNibNamed:@"GYNoAddressView" owner:nil options:nil] firstObject];
    noOrderView.frame = self.collectionView.frame;
    noOrderView.markImageView.image = [UIImage imageNamed:@"mcc_07_w"];
    noOrderView.headLable.text = @"您还没有相关的订单";
    noOrderView.footLable.text = @"可以去看看哪些想要买的";
    [noOrderView.addBtu setTitle:@"随便逛逛" forState:UIControlStateNormal];
    __weak typeof(self) weakSelf = self;
    noOrderView.addNewAddressCallBack = ^{
        weakSelf.tabBarController.selectedIndex = 0;
    };
    [self.view addSubview:noOrderView];
    
    self.noOrderView = noOrderView;
}

#pragma mark -- 支付

/**
 注册支付是否成功的监听
 */
- (void)registerPayNotification {
    //监听是微信否支付成功
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WeiXinPaySuccess) name:WeiXinPaySuccess object:nil];
    
    //监听微信支付取消
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(WeiXinPayCancel) name:WeiXinPayCanacel object:nil];
    
    //监听支付宝是否支付成功
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(zhifubaoPaySuccess:) name:zhifubaoPaySuccess object:nil];
}

/**
 移除支付方面的监听
 */
- (void)removePayNotification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:WeiXinPaySuccess object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:WeiXinPayCanacel object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:zhifubaoPaySuccess object:nil];
}

/**
 发起支付
 */
- (void)launchPay:(GYPayment *)model pay_sn:(NSString *)pay_sn notifyUrl:(NSString *)notifyUrl {
    if ([model.payment_id intValue] == 1) {
        //NSLog(@"支付宝支付....");
        [GYNetWorkTools requestLaunchPay:pay_sn payment_code:model.payment_code success:^(NSDictionary *obj) {
            //NSLog(@"zhifubao  =======%@",obj);
            int code = [[obj objectForKey:@"code"] intValue];
            if (code == 200) {
                NSString *datas = [obj objectForKey:@"datas"];
                [[AlipaySDK defaultService] payOrder:datas fromScheme:@"zhifubao" callback:^(NSDictionary *resultDic) {
                    NSLog(@"resultDic====%@",resultDic);
                }];
            }else if (code == 400) {
                [EasyTextView showText:@"支付失败请,请重新支付!"];
                [self.navigationController popViewControllerAnimated:YES];
            }
        } error:^(NSError *error) {
            [EasyTextView showText:@"支付失败请,请重新支付!"];
            [self.navigationController popViewControllerAnimated:YES];
            [EasyLodingView hidenLoding];
            NSLog(@"LaunchPayzhifubaoFailed=====%@",error);
        }];
    }else if ([model.payment_id intValue] == 2) {
        //NSLog(@"微信支付....");
        [GYNetWorkTools requestWeiXinPay:pay_sn payment_code:model.payment_code wxpay_appid:[model.payment_config objectForKey:@"wxpay_appid"] wxpay_mch_id:[model.payment_config objectForKey:@"wxpay_mch_id"] wxpay_key:[model.payment_config objectForKey:@"wxpay_key"] notifyUrl:notifyUrl success:^(NSDictionary *obj) {
            NSLog(@"obj----=====%@",obj);
            int code = [[obj objectForKey:@"code"] intValue];
            NSDictionary *datas = [obj objectForKey:@"datas"];
            if (code == 200) {
                PayReq *request = [[PayReq alloc] init];
                request.partnerId = [datas objectForKey:@"partnerid"];
                request.prepayId= [datas objectForKey:@"prepayid"];
                request.package = [datas objectForKey:@"package"];
                request.nonceStr= [datas objectForKey:@"noncestr"];
                request.timeStamp= (UInt32)[[datas objectForKey:@"timestamp"] longLongValue];
                request.sign= [datas objectForKey:@"sign"];
                [WXApi sendReq:request];
            }else {
                NSLog(@"error======%@",[[datas objectForKey:@"error"] objectForKey:@"return_msg"]);
            }
        } error:^(NSError *error) {
            NSLog(@"WeiXinPayError");
        }];
    }
}

/**
 微信取消支付回调方法
 */
- (void)WeiXinPayCancel {
    NSLog(@"微信支付取消...");
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 微信支付成功回调方法
 */
- (void)WeiXinPaySuccess {
    NSLog(@"微信支付成功回调...");
//    NSLog(@"self.pay_sn==========%@",self.pay_sn);
//    NSLog(@"self.payModel.payment_code================%@",self.payModel.payment_code);
//    NSLog(@"self.payModel.wxpay_appid================%@",[self.payModel.payment_config objectForKey:@"wxpay_appid"]);
//    NSLog(@"self.payModel.wxpay_mch_id================%@",[self.payModel.payment_config objectForKey:@"wxpay_mch_id"]);
//    NSLog(@"self.payModel.wxpay_key================%@",[self.payModel.payment_config objectForKey:@"wxpay_key"]);
//    NSLog(@"self.notifyUrl================%@",self.notifyUrl);
    [GYNetWorkTools requestWeiXinPaySuccess:self.pay_sn payment_code:self.payModel.payment_code wxpay_appid:[self.payModel.payment_config objectForKey:@"wxpay_appid"] wxpay_mch_id:[self.payModel.payment_config objectForKey:@"wxpay_mch_id"] wxpay_key:[self.payModel.payment_config objectForKey:@"wxpay_key"] notifyUrl:self.notifyUrl success:^(NSDictionary *obj) {
        //NSLog(@"obj========%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *data = [obj objectForKey:@"datas"];
        if (code == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        }else if (code == 400) {
            NSLog(@"error==========%@",[data objectForKey:@"error"]);
            [EasyTextView showText:@"支付失败,请重新支付!"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } error:^(NSError *error) {
        [EasyTextView showText:@"支付失败,请重新支付!"];
        [self.navigationController popViewControllerAnimated:YES];
        NSLog(@"WeiXinPaySuccesFailed======%@",error);
    }];
    
    //这里要返回订单查询界面或则 上平界面
}

/**
 支付宝成功回调方法
 
 @param noti <#noti description#>
 */
- (void)zhifubaoPaySuccess:(NSNotification *)noti {
    NSLog(@"支付宝支付成功回调");
    NSDictionary *payResult = noti.object;
    int resultStatus = [[payResult objectForKey:@"resultStatus"] intValue];
    if (resultStatus == 9000) {
        //代表 成功
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[[payResult objectForKey:@"result"] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
        NSDictionary *alipay_trade_app_pay_response = [result objectForKey:@"alipay_trade_app_pay_response"];
        NSString *trade_no = [alipay_trade_app_pay_response objectForKey:@"trade_no"];
        NSString *out_trade_no = [alipay_trade_app_pay_response objectForKey:@"out_trade_no"];
        
        [GYNetWorkTools requestZhifubaoPaySuccess:out_trade_no trade_no:trade_no uccess:^(NSDictionary *obj) {
            //NSLog(@"obj========%@",obj);
            int code = [[obj objectForKey:@"code"] intValue];
            if (code == 200) {
                NSLog(@"支付宝支付完成通知完成!");
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                NSLog(@"支付宝支付完成通知有误!");
            }
        } error:^(NSError *error) {
            NSLog(@"支付宝支付完成通知有误!");
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
    }else {
        //代表支付失败
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark -- 懒加载

- (NSMutableArray *)orderList {
    if (!_orderList) {
        _orderList = [NSMutableArray array];
    }
    
    return _orderList;
}

- (NSMutableArray *)searchResults {
    if (!_searchResults) {
        _searchResults = [NSMutableArray array];
    }
    
    return _searchResults;
}

- (void)dealloc {
    [self.payMethodSelectView removeFromSuperview];
    //移除支付监听者
    [self removePayNotification];
    NSLog(@"GYOrderViewController===dealloc");
}

@end
